package lium.lbs.lbsim.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.buz.hjcdriver.bean.AResultBean
import com.buz.hjcdriver.bean.BankCardListResultBean
import com.buz.hjcdriver.bean.WithdrawaShowResultBean
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.DialogCallback
import com.lmlibrary.utils.MoneyFormat
import lium.lbs.lbsim.utils.ToastUtils
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_wallet_withdrawals.*
import lium.lbs.lbsim.R
import java.math.BigDecimal
import java.util.HashMap

class WalletWithdrawalsActivity : BaseActivity(){

    override fun getLayoutId(): Int { return R.layout.activity_wallet_withdrawals  }
    var showBean:WithdrawaShowResultBean?=null
    var cardBean:BankCardListResultBean?=null
    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("提现")
        MoneyFormat.setPricePoint(et_money_text)
        addOnClickListeners(R.id.setall,R.id.choose_bankcard_layout,R.id.btn_submit)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v!!.id){
            R.id.setall -> {
                showBean?.let {
                    et_money_text.setText(showBean!!.money)
                }
            }
            R.id.choose_bankcard_layout -> {
                val intent = Intent(this, BankCardListActivity::class.java)
                intent.putExtra("isChooseBankCard",true)
                startActivityForResult(intent,11)
            }
            R.id.btn_submit ->{
                submit()
            }
        }
    }

    private fun submit() {
        if (TextUtils.isEmpty(et_money_text.text.toString())){
            ToastUtils.showToast("请输入提现金额")
            return
        }
        if(BigDecimal(et_money_text.text.toString()) <= BigDecimal.ZERO){ //是否等于0
            ToastUtils.showToast("提现金额无效")
            return
        }

        val param = HashMap<String, String>()
        showBean?.let {
            param["bankId"]=showBean!!.id
        }
        cardBean?.let {
            param["bankId"]=cardBean!!.id
        }
        param["price"]=et_money_text.text.toString()
        postData("/member/tiXianDingdan",param, object : DialogCallback<ResponseBean<AResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                ToastUtils.showToast(response.body().msg)
                setResult(200)
                finish()
            }
        })
    }
    override fun onResume() {
        super.onResume()
        tiXianShow()
    }
    private fun tiXianShow() {
        val param = HashMap<String, String>()
        postData("/member/tiXianShow",param, object : DialogCallback<ResponseBean<WithdrawaShowResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<WithdrawaShowResultBean>>) {
                if (!TextUtils.isEmpty(response.body().data.cardNum)){
                    showBean = response.body().data
                    money.text = "可用"+resources.getString(R.string.text_rmb_sign)+response.body().data.money
                    bkname.text=response.body().data.bkname
                    cardNum.text=response.body().data.cardNum
                    name.text=response.body().data.name
                    phone.text=response.body().data.phone+""

                }
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (data?.hasExtra("bean")!!){
                cardBean = data.getSerializableExtra("bean") as BankCardListResultBean?
                cardNum.text=cardBean?.cardNum
                name.text=cardBean?.name
            }
        }
    }
    override fun getNetWorkData() {
    }

}